/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.ucenter;

import com.jfinal.config.Routes;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Directive;
import com.jfinal.template.Engine;
import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceManager;
import com.lambkit.core.rpc.RpcConfig;
import com.lambkit.db.datasource.ActiveRecordPluginWrapper;
import com.lambkit.module.LambkitModule;

import com.lambkit.module.ucenter.UcenterConfig;
import com.lambkit.module.ucenter.rpc.model.UcenterOauth;
import com.lambkit.module.ucenter.rpc.model.UcenterUser;
import com.lambkit.module.ucenter.rpc.model.UcenterUserDetails;
import com.lambkit.module.ucenter.rpc.model.UcenterUserLog;
import com.lambkit.module.ucenter.rpc.model.UcenterUserOauth;
import com.lambkit.module.ucenter.rpc.api.UcenterOauthService;
import com.lambkit.module.ucenter.rpc.api.UcenterUserService;
import com.lambkit.module.ucenter.rpc.api.UcenterUserDetailsService;
import com.lambkit.module.ucenter.rpc.api.UcenterUserLogService;
import com.lambkit.module.ucenter.rpc.api.UcenterUserOauthService;
import com.lambkit.module.ucenter.service.impl.UcenterOauthServiceImpl;
import com.lambkit.module.ucenter.service.impl.UcenterOauthServiceMock;
import com.lambkit.module.ucenter.service.impl.UcenterUserServiceImpl;
import com.lambkit.module.ucenter.service.impl.UcenterUserServiceMock;
import com.lambkit.module.ucenter.service.impl.UcenterUserDetailsServiceImpl;
import com.lambkit.module.ucenter.service.impl.UcenterUserDetailsServiceMock;
import com.lambkit.module.ucenter.service.impl.UcenterUserLogServiceImpl;
import com.lambkit.module.ucenter.service.impl.UcenterUserLogServiceMock;
import com.lambkit.module.ucenter.service.impl.UcenterUserOauthServiceImpl;
import com.lambkit.module.ucenter.service.impl.UcenterUserOauthServiceMock;
import com.lambkit.module.ucenter.web.directive.UcenterOauthDirective;
import com.lambkit.module.ucenter.web.directive.UcenterUserDirective;
import com.lambkit.module.ucenter.web.directive.UcenterUserDetailsDirective;
import com.lambkit.module.ucenter.web.directive.UcenterUserLogDirective;
import com.lambkit.module.ucenter.web.directive.UcenterUserOauthDirective;
import com.lambkit.module.ucenter.web.tag.UcenterOauthMarker;
import com.lambkit.module.ucenter.web.tag.UcenterUserMarker;
import com.lambkit.module.ucenter.web.tag.UcenterUserDetailsMarker;
import com.lambkit.module.ucenter.web.tag.UcenterUserLogMarker;
import com.lambkit.module.ucenter.web.tag.UcenterUserOauthMarker;

/**
 * @author Henry 杨勇 (孤竹行) 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-11-05
 * @version 1.0
 * @since 1.0
 */
public class UcenterModule extends LambkitModule  {

	@Override
	public void configMapping(ActiveRecordPluginWrapper arp) {
		if(StrKit.isBlank(getConfig().getDbconfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configMapping(String name, ActiveRecordPluginWrapper arp) {
		super.configMapping(name, arp);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbconfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		me.addDirective("ucenterOauth", UcenterOauthDirective.class);
		me.addDirective("ucenterUser", UcenterUserDirective.class);
		me.addDirective("ucenterUserDetails", UcenterUserDetailsDirective.class);
		me.addDirective("ucenterUserLog", UcenterUserLogDirective.class);
		me.addDirective("ucenterUserOauth", UcenterUserOauthDirective.class);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		addTag(this);
		if("server".equals(getConfig().getServerType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getServerType())) {
			registerRemoteService();
		} 
	}
	
	public void mapping(ActiveRecordPluginWrapper arp) {
		arp.addMapping("ucenter_oauth", "oauth_id", UcenterOauth.class);
		arp.addMapping("ucenter_user", "user_id", UcenterUser.class);
		arp.addMapping("ucenter_user_details", "user_id", UcenterUserDetails.class);
		arp.addMapping("ucenter_user_log", "user_log_id", UcenterUserLog.class);
		arp.addMapping("ucenter_user_oauth", "user_oauth_id", UcenterUserOauth.class);
	}
	
	public void addTag(LambkitModule lk) {
		lk.addTag("ucenterOauth", new UcenterOauthMarker());
		lk.addTag("ucenterUser", new UcenterUserMarker());
		lk.addTag("ucenterUserDetails", new UcenterUserDetailsMarker());
		lk.addTag("ucenterUserLog", new UcenterUserLogMarker());
		lk.addTag("ucenterUserOauth", new UcenterUserOauthMarker());
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
		ServiceManager.me().mapping(UcenterOauthService.class, UcenterOauthServiceImpl.class, UcenterOauthServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UcenterUserService.class, UcenterUserServiceImpl.class, UcenterUserServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UcenterUserDetailsService.class, UcenterUserDetailsServiceImpl.class, UcenterUserDetailsServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UcenterUserLogService.class, UcenterUserLogServiceImpl.class, UcenterUserLogServiceMock.class, group, version, port);
		ServiceManager.me().mapping(UcenterUserOauthService.class, UcenterUserOauthServiceImpl.class, UcenterUserOauthServiceMock.class, group, version, port);
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
		ServiceManager.me().remote(UcenterOauthService.class, UcenterOauthServiceMock.class, group, version, port);
		ServiceManager.me().remote(UcenterUserService.class, UcenterUserServiceMock.class, group, version, port);
		ServiceManager.me().remote(UcenterUserDetailsService.class, UcenterUserDetailsServiceMock.class, group, version, port);
		ServiceManager.me().remote(UcenterUserLogService.class, UcenterUserLogServiceMock.class, group, version, port);
		ServiceManager.me().remote(UcenterUserOauthService.class, UcenterUserOauthServiceMock.class, group, version, port);
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getVersion();
	}
	public UcenterConfig getConfig() {
		return Lambkit.config(UcenterConfig.class);
	}
}

